//
//  OffLineUploadReadMeterRecordHelper.swift
//  Employee
//
//  Created by tancheng on 2018/6/28.
//  Copyright © 2018年 remotevillage. All rights reserved.
//

import Foundation
import FMDB


class OffLineUploadReadMeterRecordHelper {
    
    static let shared: OffLineUploadReadMeterRecordHelper = {
        
        let helper = OffLineUploadReadMeterRecordHelper()
        
        helper.createTable()
        
        return helper
        
    }()
    
    //
    fileprivate var readMeterRecordPath: String {
        
        let docuPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    
        return docuPath + "/" + "OffLineUploadReadMeterRecord.db"
    }
    
    fileprivate var tableName: String {
        return "meter_read"
    }
    
    ///字段名称
    fileprivate var fieldnames:(
        taskId: String,
        meterRead: String,
        fileIds: String
        )
    {
        return (
            "taskId",
            "meterRead",
            "fileIds"
        )
    }
    
    fileprivate var dataBase: FMDatabase?
    
    fileprivate func createTable() {
        
        dataBase = FMDatabase(path: readMeterRecordPath)
        
        guard let db = dataBase  else {
            return
        }
        guard db.open() else {
            return
        }
        
        let sql = "create table if not exists \(tableName) ('ID' INTEGER PRIMARY KEY AUTOINCREMENT,'\(fieldnames.taskId)' TEXT NOT NULL, '\(fieldnames.meterRead)' TEXT NOT NULL,'\(fieldnames.fileIds)' )"
        //BOLB NOT NULL
        
        if !db.executeUpdate(sql, withArgumentsIn: []) {
            print("数据库创建失败")
        }
        
        db.close()
        
    }
    
    @discardableResult
    func saveRecord(_ params:[Any]) -> Bool {
        
        guard let db = dataBase  else {
            return false
        }
        guard db.open() else {
            return false
        }
        
        let sql = "insert into '\(tableName)'(\(fieldnames.taskId),\(fieldnames.meterRead),\(fieldnames.fileIds)) values(?,?,?)"
        
        
        guard db.executeUpdate(sql, withArgumentsIn: params) else {
            db.close()
            return false
        }
        
        db.close()
        
        return true
    }
    //多条抄表记录
    func getRecord() -> [[Any]] {
        
        guard let db = dataBase  else {
            return []
        }
        guard db.open() else {
            return []
        }
        
        let sql = "select * from '\(tableName)'"
        
        
        guard let reuslt = db.executeQuery(sql, withArgumentsIn: []) else {
            return []
        }
        
        
        var reusltData: [[Any]] = []
        
        while reuslt.next() {
            
            let element = [
                reuslt.string(forColumn: "\(fieldnames.taskId)") ?? "",
                reuslt.string(forColumn: "\(fieldnames.meterRead)") ?? "",
                reuslt.object(forColumn: "\(fieldnames.fileIds)") ?? []
            ]
            
            reusltData.append(element)
        
        }
        
        reuslt.close()
        db.close()
        
        return reusltData
    }
    
    @discardableResult
    func delete(with taskId:String) -> Bool {
        
        guard let db = dataBase  else {
            return false
        }
        guard db.open() else {
            return false
        }
        
        let sql = "delete from '\(tableName)' where \(fieldnames.taskId) = ?"
        
        
        guard db.executeUpdate(sql, withArgumentsIn: [taskId]) else {
            db.close()
            return false
        }
        
        db.close()
        
        return true
        
    }
    
    
}
